﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Globalization;
using System.Text.RegularExpressions;
namespace DelicateDict.Classes
{
    class WordList
    {


        string WordFile = string.Empty;
        public  List<string> ArrayWord;
        public WordList()
        {
            IOFile iof = new IOFile();
            WordFile = iof.DefaultDictType.DBWord;
            LoadALLWord();
        }
        public WordList(string filePath)
        {
            WordFile = filePath;
            LoadALLWord();
        }
      
         public void CloseWordList()
           {
             ArrayWord.Clear();
             ArrayWord = null;

         }
        public string[] LoadWordList(int start, int length)
        {
            string[] wordlist = null;
            int i = 0;
            wordlist = new string[length];
            do
            {
                try
                {
                    wordlist[i] = ArrayWord[start + i];
                    i++;
                }
                catch (ArgumentOutOfRangeException)
                {
                    List<string> l = new List<string>();
                    foreach (string str in wordlist)
                    {
                        if (str != null)
                        {
                            l.Add(str);
                        }
                        else break;
                    }
                    return l.ToArray();
                }

            } while (i < length);

            return wordlist;
        }
        public void LoadALLWord()
        {
            ArrayWord = new List<string>();
            StreamReader rd = new StreamReader(WordFile);
            string line = string.Empty;
             
                     while((line = rd.ReadLine())!= null)
                     {
                        ArrayWord.Add(line);
                     }

           // ArrayWord.Sort(new CompareMystring());
            rd.Close();
                    
        }

    }
    public class CompareMystring : IComparer<string>
    {
        CompareInfo ci = null;
        public CompareMystring()
        {
            ci = CultureInfo.GetCultureInfo("").CompareInfo;
        }
        public int Compare(string x, string y)
        {
            return ci.Compare((string)x, (string)y, CompareOptions.OrdinalIgnoreCase);
        }
    }
    
}
